package com.sizzlevoice.sizzlehome;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateRecipeActivity extends Activity {

	//TODO: Create GUI to have Recipe Name, Recipe Ingredients, and Recipe Directions Edit Text Boxes
	//TODO: Store information in XML file with format tags such as <name>, <ingredients>, <directions>
	//TODO: Read information from XML file - I have code to do this
	//TODO: Add this activity in the Manifest File
	
	EditText recipe;
	EditText ingredients;
	EditText directions;
	String recipeText;
	String ingredientsText;
	String directionsText;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_simmer_recipe);
	    
	    recipe = (EditText) findViewById(R.id.recipename);
	    ingredients = (EditText) findViewById(R.id.ingredients);
	    directions = (EditText) findViewById(R.id.directions);
	    
        /**searchBtn
         * This button should only be used to help the user search for recipes**/
        Button saveBtn = (Button) findViewById(R.id.save);
        saveBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				save();
				}
			});
	}
	
	public void save() {
		recipeText = recipe.getText().toString();
		ingredientsText = ingredients.getText().toString();
		directionsText = directions.getText().toString();
		
		String XMLheader = "<?xml version=\"1.0\"?>"; 
		String recipeHeader = "<recipexml version=\"1.0\">";
		String recipeEndHeader = "</recipexml>";
		String recipeTag = "<recipe>";
		String recipeEndTag = "</recipe>";
		String ingredientsTag = "<ingredients>";
		String ingredientsEndTag = "</ingredients>";
		String directionsTag = "<directions>";
		String directionsEndTag = "</directions>";
		
		
		//if recipe is not null or empty
		if(!recipeText.equalsIgnoreCase("") || !recipeText.equalsIgnoreCase(null)){
			try{
				File sdCard = Environment.getExternalStorageDirectory();
				File directory = new File (sdCard.getAbsolutePath() + "/Recipes/");
				directory.mkdirs();
				
				//Path and name of XML file
				File file = new File(directory, recipeText + ".xml");
				FileOutputStream fOut = new FileOutputStream(file);
				OutputStreamWriter osw = new OutputStreamWriter(fOut);
				
				//Example of what xml file should look like
				//<?xml version="1.0"?>
				//<recipexml version="1.0">
				//<recipe>Pizza<ingredients>Bagel and Sauce</ingredients><directions>Cook in oven</directions></recipe>
				//</recipesxml>
				
				//Write XML File
				osw.write(XMLheader);
				osw.write('\n');
				osw.write(recipeHeader);
				osw.write('\n');
				osw.write(recipeTag);
				osw.write(recipeText);
				osw.write(ingredientsTag);
				osw.write(ingredientsText);
				osw.write(ingredientsEndTag);
				osw.write(directionsTag);
				osw.write(directionsText);
				osw.write(directionsEndTag);
				osw.write(recipeEndTag);
				osw.write('\n');
				osw.write(recipeEndHeader);
				
				osw.flush();
				osw.close();
				fOut.close();
				
				Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show();
				
			}catch(IOException ioe){
				ioe.printStackTrace();
			}
			
			
			
		}
		
	}

	/**
     * This method is called every time this activity is called into view
     */
    public void onStart(){
    	super.onStart();
    }

}
